<?php  
//if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hello_controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		
		$temp = $this->session->userdata('is_logged_in');
		if(!isset($temp) || !$temp) exit('No direct script access allowed');
				
		$this->load->model('hello_model');
		$this->load->model('area_model');
	}


	function to_create_hello()
	{
//    redirect('map-geolocation');
	//	if(!$this->session->userdata('isverified'))
	//	{
	//		redirect('feed_controller');
			
	//	}
		//$this->load->view('map_view');
		$data['title']="Post New Hello";
		$data['id']="new";
		$data['area']=$this->area_model->ChangeQueryResultToArray($this->area_model->getAll());
		$this->load->view('header',$data);
		$this->load->view('create_hello_form',$data);
		$this->load->view('footer',$data);
	}
	function choose_place()
	{
		$data['lat']=$this->input->get('lat');
		$data['longi']=$this->input->get('longi');
		$data['title']="Post New Hello";
		$data['id']="";
		$data['area']=$this->area_model->ChangeQueryResultToArray($this->area_model->get_near($data['lat'],$data['longi']) );
		$this->load->view('header',$data);
		$this->load->view('choose_place_view',$data);
		$this->load->view('footer',$data);
	}
	function to_fill_form()
	{
		$data['title']="Post New Hello";
		$data['id']="";
		$data['areaid']=$this->input->get('areaid');
		$data['areaname']=$this->input->get('areaname');
		$this->load->view('header',$data);
		$this->load->view('create_hello_form',$data);
		$this->load->view('footer',$data);
	}

	function create_new_area()
	{
		$area = $this->area_model->create();
		redirect('hello_controller/to_fill_form?areaid='. $area->areaid .'&areaname='. $area->areaname.' ');
	}
	function create_hello()
	{
		// field name, error message, validation rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[4]');
		$this->form_validation->set_rules('location', 'Location', 'trim|required|min_length[1]|max_length[32]');
		$this->form_validation->set_rules('time', 'Time', 'trim|required');
		$this->form_validation->set_rules('date', 'Date', 'trim|required');


		if($this->form_validation->run() == FALSE)
		{
			$this->to_create_hello();
		}
		else
		{
			if( $query = $this->hello_model->create() )
			{
				redirect('feed_controller/index');
			}
			else
			{
				$this->to_create_hello();
			}
		}
	}
	function feeling_lucky()
	{
		redirect('get_position_for_lucky');
	}
	function get_lucky_hello()
	{
		$lat = $this->input->get('lat');
		$longi = $this->input->get('longi');
		$lucky = $this->hello_model->ChangeQueryResultToArray( $this->hello_model->get_lucky_hello($lat, $longi) );
		$luckyid = $lucky[0]->helloid;
		if(isset($luckyid))
			redirect('feed_controller/to_apply?helloID='.(string)($luckyid) );
		else
			redirect('feed_controller/index');
	}
	function my_waiting()
	{
		$data['title']="Hello World";
		$data['id']="feed";
		$data['rows'] = $this->hello_model->ChangeQueryResultToArray($this->hello_model->get_waiting_with_fullname());
		
		$this->load->view('header',$data);
		$this->load->view('feed_view', $data);
		$this->load->view('footer',$data);
	}

}


